package com.luo.controller;


import com.luo.pojo.po.Products;
import com.luo.pojo.po.Results;
import com.luo.service.IProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luo
 * @since 2025-06-24
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/products")
@RequiredArgsConstructor
@CrossOrigin("*")
public class ProductsController {
    private final IProductsService productsService;

    @ApiOperation("查询商品")
    @GetMapping
    public Results Inquire(@ApiParam ("商品名称") @RequestParam (required = false) String  keyword){
        return Results.success(productsService.Inquire(keyword));
    }

    @ApiOperation("添加商品")
    @PostMapping
    public Results add(@RequestBody Products products){
        return productsService.save(products) ? Results.success() : Results.error("添加失败");
    }

    @ApiOperation("修改商品")
    @PutMapping
    public Results update(@RequestBody Products products){
        return productsService.updateById(products) ? Results.success() : Results.error("修改失败");
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/{id}")
    public Results delete(@PathVariable Integer id){
        return productsService.removeById(id) ? Results.success() : Results.error("删除失败");
    }



}
